local sk__jinlong = fk.CreateSkill {

  name = "sk__jinlong",

  tags = { Skill.Compulsory, },

}



sk__jinlong:addEffect(fk.AfterCardsMove, {
  anim_type = "special",
  derived_piles = "sk__jinlong",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__jinlong.name) then
      local cards = {}
      for _, move in ipairs(data) do
        if (move.to == player and (move.toArea == Card.PlayerHand or move.toArea == Card.PlayerEquip)) or
          move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).type == Card.TypeEquip then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      cards = player.room.logic:moveCardsHoldingAreaCheck(cards)
      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self)
    player:addToPile(sk__jinlong.name, cards, true, sk__jinlong.name, player)
    if player.dead then return end
    player:drawCards(#cards, sk__jinlong.name)
    if player.dead then return end
    for _, id in ipairs(player:getPile(sk__jinlong.name)) do
      local card = Fk:getCardById(id)
      if card.equip_skill and not player:hasSkill(card.equip_skill.name) then
        room:handleAddLoseSkills(player, card.equip_skill.name, nil, false, true)
      end
    end
  end,
})
sk__jinlong:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from and move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerSpecial and info.fromSpecialName == "sk__jinlong" then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local skills = {}
    for _, skill in ipairs(player.player_skills) do
      if skill:isEquipmentSkill(player) then
        table.insert(skills, skill.name)
      end
    end
    if #player:getCardIds("e") > 0 then
      for _, id in ipairs(player:getCardIds("e")) do
        local card = Fk:getCardById(id)
        if card.equip_skill then
          table.removeOne(skills, card.equip_skill.name)
        end
      end
    end
    player.room:handleAddLoseSkills(player, "-"..table.concat(skills, "|-"), nil, false, true)
  end,
})

sk__jinlong:addEffect("atkrange", {
  name = "#sk__jinlong_attackrange",
  correct_func = function (self, from, to)
    if #from:getPile("sk__jinlong") > 0 and from:hasSkill(sk__jinlong.name) then
      local nums = {}
      for _, id in ipairs(from:getPile("sk__jinlong")) do
        if Fk:getCardById(id).sub_type == Card.SubtypeWeapon then
          table.insert(nums, Fk:getCardById(id).attack_range)
        end
      end
      if #nums > 0 then
        return math.max(table.unpack(nums)) - 1
      end
    end
    return 0
  end,
})

return sk__jinlong